package com.jf.concurrent.queue.demo;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import static java.lang.System.out;
import static java.lang.Thread.currentThread;

/**
 * 生产者
 *
 * @author Junfeng
 */
public class Producer implements Runnable {

    private Topic topic;

    public Producer(Topic topic) {
        this.topic = topic;
    }

    @Override
    public void run() {
        while (Controller.FLAG) {
            try {
                TimeUnit.SECONDS.sleep(1);
                String s = currentThread().getName() + "\t" + new Random().nextInt(100);
                boolean offer = topic.offer(s);
                out.println(currentThread().getName() + "\t 生产成功 " + offer + " - " + s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        out.println(currentThread().getName() + "\t 停止生产");
    }
}
